import re
import requests
from bs4 import BeautifulSoup
import csv
f=open("xinxi.csv" , 'w' ,newline="")
csvwriter = csv.writer(f)
csvwriter.writerow(['岗位名称' ,"岗位链接","岗位薪资","岗位地址","岗位年限","岗位学历","岗位职责","岗位要求"])
for i in range(1,11):
    print(f"正在爬取第{i}页")
    url = 'https://www.qlrc.com/personal/js/search/{i}?keywords=%E6%95%B0%E6%8D%AE'
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4621.0 Safari/537.36"
    }
    res = requests.get(url, headers=header)
    res.encoding = res.apparent_encoding
    html = res.text
    soup = BeautifulSoup(html, "html.parser")
    gangweis = soup.find_all('div', attrs={"class": "jobname"})
    requires = soup.find_all('div', attrs={"class": "jobrequire"})
    gangweixinxi = []
    for i in range(len(gangweis)):
        xinXi = []
        gangwei = gangweis[i].a.text.strip()
        xinXi.append(gangwei)
        lianjie = "https://www.qlrc.com" + gangweis[i].a["href"]
        xinXi.append(lianjie)
        # print(xinXi)
        require = requires[i].children
        for child in require:

            text = child.text.replace(" ", "")
            text = text.replace("\xa0", "")
            text = text.replace("|", "")
            if len(text) > 0:
                xinXi.append(text)
        gangweixinxi.append(xinXi)
    for i in range(len(gangweixinxi)):
        url1 = gangweixinxi[i][1]
        res1 = requests.get(url1, headers=header)
        res1.encoding = res1.apparent_encoding
        html1 = res1.text
        obj = re.compile(r"<p class='text-content' data-v-\w{8}>(?P<gangweixiangqing>.*?)</p>")
        result = obj.finditer(html1)
        for item in result:
            gangweixinxi[i].append(item.group('gangweixiangqing'))
    print(gangweixinxi)
    for info in gangweixinxi:
        gangwei_name = info[0]
        gangwei_lianjie = info[1]
        gangwei_xinzi = info[2]
        gangwei_dizhi = info[3]
        gangwei_nianxian = info[4]
        gangwei_xueli = info[5]
        gangwei_zhize = info[6]
        gangwei_yaoqiu = info[7]
        csvwriter.writerow([gangwei_name, gangwei_lianjie
                               , gangwei_xinzi, gangwei_dizhi, gangwei_nianxian, gangwei_xueli,
                            gangwei_zhize, gangwei_yaoqiu])
f.close()
print("over")
